package cn.org.exam.mapper;

import cn.org.exam.model.entity.Role;
import cn.org.exam.model.entity.User;
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户 Mapper，对应 sys_user 表
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    // 登录用：必须手写 SQL，避免 MyBatis-Plus 自动 select *
    @Select("""
            SELECT 
                id,
                username,
                password,
                nickname,
                enabled,
                create_time,
                update_time
            FROM sys_user
            WHERE username = #{username}
            LIMIT 1
            """)
    User selectByUsername(@Param("username") String username);


    // 查角色
    @Select("""
            SELECT r.id, r.code, r.name
            FROM sys_role r
            JOIN sys_user_role ur ON ur.role_id = r.id
            WHERE ur.user_id = #{userId}
            """)
    List<Role> listRolesByUserId(@Param("userId") Long userId);
}


